/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xdxn.editor.studio.biz.barrage;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import xdxn.editor.studio.data.ProjectManager;
import xdxn.editor.studio.data.barrage.Barrage;
import xdxn.editor.studio.utils.IDFactory;

/**
 *
 * @author Cai Han
 */
public class BarrageTableModel extends AbstractTableModel {
    
    private final IDFactory _idFactory;
    private final ArrayList<Barrage> _list;
    private final String[] _columnNames = {"id", "描述"};

    public BarrageTableModel(ArrayList<Barrage> list) {
        this._list = list;
        _idFactory = new IDFactory();
    }

    @Override
    public int getRowCount() {
        return this._list.size();
    }

    @Override
    public int getColumnCount() {
        return this._columnNames.length;
    }

    @Override
    public void setValueAt(Object obj, int rowIndex, int columnIndex) {
        Barrage barrage = this._list.get(rowIndex);
        if (this._columnNames[columnIndex].equals("描述")) {
            barrage.name = obj.toString();
        }
        super.setValueAt(obj, rowIndex, columnIndex);
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Barrage barrage = this._list.get(rowIndex);
        switch (this._columnNames[columnIndex]) {
            case "名称":
                return barrage.name;
            case "id":
                return barrage.id;
        }
        return null;
    }

    @Override
    public String getColumnName(int i) {
        return this._columnNames[i];
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return !(column == 0);
    }

    public Barrage add(int rowIndex) {
        //
        Barrage barrage = new Barrage();
        barrage.id = this._idFactory.requestID();
        barrage.name = "弹幕 " + barrage.id;
        //
        int size = this._list.size();
        if (rowIndex < 0 || rowIndex > size - 1) {
            this._list.add(barrage);
            fireTableRowsInserted(size, size);
        } else {
            this._list.add(rowIndex, barrage);
            fireTableRowsInserted(rowIndex, rowIndex);
        }
        // 设置当前弹幕对象
        ProjectManager.getInstance().current().current_barrage = barrage;
        
        return barrage;
    }

    public boolean update(int rowIndex, Barrage barrage) {
        if (this._list.isEmpty() || rowIndex < 0 || rowIndex > this._list.size() - 1) {
            return false;
        }

        this._list.set(rowIndex, barrage);
        fireTableRowsUpdated(rowIndex, rowIndex);
        
        return true;
    }

    public boolean remove(int rowIndex){
        if (this._list.isEmpty() || rowIndex < 0 || rowIndex > this._list.size() - 1) {
            return false;
        }
        _list.remove(rowIndex);
        this.fireTableRowsDeleted(rowIndex, rowIndex);
        return true;
    }

    
}
